//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
/// \file optical/wls/src/WLSRunAction.cc
/// \brief Implementation of the WLSRunAction class
//
//

#include "WLSRunAction.hh"
#include "WLSEventAction.hh"

#include "WLSDetectorConstruction.hh"
#include "WLSRun.hh"
#include "WLSSteppingAction.hh"

#include "G4AnalysisManager.hh"
#include "G4Run.hh"
#include "G4RunManager.hh"
#include "Randomize.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

WLSRunAction::WLSRunAction(WLSEventAction *eventaction)
    : fEventAction(eventaction)
{
  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->SetDefaultFileType("root");
  // If the filename extension is not provided, the default file type (root)
  // will be used for all files specified without extension.
  analysisManager->SetVerboseLevel(1);
  analysisManager->SetNtupleMerging(true);
  // Note: merging ntuples is available only with Root output
  analysisManager->SetFileName("psfhisto");
  // Book histograms, ntuple
  if (fEventAction)
  {
    // Creating ntuple
    analysisManager->CreateNtuple("ntuple", "Time and index of detector");                   // 创建元组
    analysisManager->CreateNtupleDColumn("Energy", fEventAction->GetEnergy());               // 探测器探测光子能量
    analysisManager->CreateNtupleDColumn("Time", fEventAction->GetTime());                   // 探测器探测光子时间
    analysisManager->CreateNtupleIColumn("DetectorIndex", fEventAction->GetDetectorIndex()); // 探测器编号
    analysisManager->CreateNtupleIColumn("NPhoton", fEventAction->GetNPhoton());             // 探测器探测光子数
    analysisManager->CreateNtupleDColumn("MuonEdep", fEventAction->GetMuonEdep());           // 缪子沉积能量
    analysisManager->CreateNtupleIColumn("NPPhoton", fEventAction->GetNPPhoton());           // 产生初始光子数
    analysisManager->CreateNtupleDColumn("FiberZ", fEventAction->GetFiberZ());               // 产生初始光子数
    analysisManager->FinishNtuple();
  }
  // Set ntuple output file
  // analysisManager->SetNtupleFileName(0, "psfntuple");
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

G4Run *WLSRunAction::GenerateRun()
{

  fRun = new WLSRun();
  return fRun;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void WLSRunAction::BeginOfRunAction(const G4Run *)
{

  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->Reset();
  analysisManager->OpenFile(); // do NOT insert fileName, otherwise it will not accept the name from the .mac file
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void WLSRunAction::EndOfRunAction(const G4Run *)
{

  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->Write();
  analysisManager->CloseFile(false);

  if (isMaster)
  {
    fRun->EndOfRun();
  }
}
